package com.haobangs.imms.smsUtils;


import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.tea.TeaException;
import com.aliyun.teaopenapi.models.Config;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;

/**
 * @Author HaoBangs
 * @Date 2023年 07月 02日 01: 50
 * @Description 阿里云短信推送
 **/
@Slf4j
@Service
public class AliyunSmsService {
    @Value("${aliyun.sms.accessKeyId}")
    private String accessKeyId;
    @Value("${aliyun.sms.accessKeySecret}")
    private String accessKeySecret;
    @Value("${aliyun.sms.endpoint}")
    private String endpoint;
    @Value("${aliyun.sms.signName}")
    private String signName;
    @Value("${aliyun.sms.templateCode}")
    private String templateCode;

    /**
     * 使用 AK&ASK 初始化账号 Client
     *
     * @param accessKeyId
     * @param accessKeySecret
     * @return Client
     * @throws Exception 短信推送异常
     */
    public static Client createClient(String accessKeyId, String accessKeySecret, String endpoint)
            throws Exception {
        Config config =
                new Config()
                        // 必填，您的 AccessKey ID
                        .setAccessKeyId(accessKeyId)
                        // 必填，您的 AccessKey Secret
                        .setAccessKeySecret(accessKeySecret);
        // 访问的域名
        config.endpoint = endpoint;
        return new com.aliyun.dysmsapi20170525.Client(config);
    }

    /**
     * 发送短信推送
     *
     * @param phone 电话号码
     * @throws Exception 短信推送异常
     */
    public void sendCodeAsync(String phone, String factoryName, String alarmInfo, String edgeTime) {

            try {
                Client client = createClient(this.accessKeyId, this.accessKeySecret, this.endpoint);
                Map<String, Object> param = new HashMap<>();
                param.put("deviceName", factoryName);
                param.put("alarmInfo", alarmInfo);
                param.put("edgeTime", edgeTime);
                SendSmsRequest sendSmsRequest = new SendSmsRequest()
                        .setSignName(this.signName)
                        .setTemplateCode(this.templateCode)
                        .setPhoneNumbers(phone)
                        .setTemplateParam(JSONObject.toJSONString(param));

                log.info("发送短信入参: " + JSONUtil.toJsonStr(sendSmsRequest));
                SendSmsResponse sendSmsResponse = client.sendSms(sendSmsRequest);
                log.info("发送短信结果: " + JSONUtil.toJsonStr(sendSmsResponse));

                if (Objects.nonNull(sendSmsResponse) && sendSmsResponse.getBody().code.equals("OK")) {
                    log.info("发送短信成功 ");
                }
            } catch (TeaException error) {
                log.error("短信推送异常结果: " + error.message);
            } catch (Exception e) {
                TeaException error = new TeaException(e.getMessage(), e);
                com.aliyun.teautil.Common.assertAsString(error.message);
                log.error("短信推送异常结果: " + error.message);
            }

    }

}
